<?php
/**
 *
 * 作    者：安德玛
 * 版本控制：
			20210209----一级推荐奖励中添加一些字段
 * 功能说明：通用放行控制器。
 *
 **/
namespace Admin\Controller;

class CommonController extends ComController
{
    /**
     * 会员一级推荐明细
     */
    public function ftuijian()
    {
        $p = isset($_GET['p']) ? intval($_GET['p']) : '1';
        $uid = I('get.uid','','htmlspecialchars');
        $level = I('get.level','','htmlspecialchars');
        $m = M('user');
        if($uid){
            $where['pid'] = $uid;
        }
        if($level){
            $where['level_id'] = $level;
        }
        $pagesize =20;#每页数量
        $offset = $pagesize * ($p - 1);//计算记录偏移量
        $count = $m->where($where)->count();
        $list = $m->order('t desc')->where($where)->limit($offset . ',' . $pagesize)->select();
        foreach ($list as $k=>&$v){
            // 会员等级
            $user_level = M('user_level')->where('id='.$v['level_id'])->find();
            $list[$k]['level_name'] = $user_level['title'];
			$id = $v['id'];
			$tixian_income = M('tixian')->where(array('uid'=>$id,'status'=>2))->sum('tixian_money');
			if(!$tixian_income){
				$tixian_income = 0;
			}
			$list[$k]['tixian_income'] =$tixian_income;
			
			//任务分佣金额
			$where['uid'] = $id;
			$where['type'] = array('in','1,2,3');
			$task_income = M('user_income')->where($where)->sum('money');
			if(!$task_income){
				$task_income = 0;
			}
			$list[$k]['task_income'] =$task_income;
			//邀请奖励，登录，充值，提现，发布次数
			$user_info = M('user')->where('id='.$id)->field('money_invite,count,mylevel_num,cash_num,fabu_num')->find();
			$list[$k]['money_invite'] =$user_info['money_invite'];
			$list[$k]['count'] =$user_info['count'];
			$list[$k]['mylevel_num'] =$user_info['mylevel_num'];
			$list[$k]['cash_num'] =$user_info['cash_num'];
			$list[$k]['fabu_num'] =$user_info['fabu_num'];
        }
        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();
        // 会员等级
        $user_level_list = M('user_level')->order('o asc')->select();
        $this->assign('level_list',$user_level_list);
        $this->assign('list', $list);
        $this->assign('page', $page);
        $this->assign('level',$level);
        $this->assign('uid',$uid);
        $this->assign('p',$p);
        $this->display();
    }
    
    /**
     * 会员二级推荐明细
     */
    public function stuijian()
    {
        $p = isset($_GET['p']) ? intval($_GET['p']) : '1';
        $uid = I('get.uid','','htmlspecialchars');
        $level = I('get.level','','htmlspecialchars');
        $model = M();
        $sqlWhere = "1=1 and ";
        if($uid){
            $sqlWhere .= "pid in (select id from ".C('DB_PREFIX')."user where pid='$uid') and ";
        }
        if($level){
            $sqlWhere .= "level_id='{$level}' and ";
        }
        $sqlWhere = msubstr($sqlWhere, 0, utf8_strlen($sqlWhere) - 4, "utf-8", false);
        $pagesize = 20;#每页数量
        $offset = $pagesize * ($p - 1);//计算记录偏移量
        $count = $model->query("select count(*) as total from ".C('DB_PREFIX')."user where ".$sqlWhere);
        $count = $count[0]['total'];
        $sql = "select * from ".C('DB_PREFIX')."user where ".$sqlWhere." order by t desc limit ".$offset . "," . $pagesize;
        $list = $model->query($sql);
        foreach ($list as $k=>&$v){
            // 会员等级
            $user_level = M('user_level')->where('id='.$v['level_id'])->find();
            $list[$k]['level_name'] = $user_level['title'];
        }
        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();
        // 会员等级
        $user_level_list = M('user_level')->order('o asc')->select();
        $this->assign('level_list',$user_level_list);
        $this->assign('list', $list);
        $this->assign('page', $page);
        $this->assign('level',$level);
        $this->assign('uid',$uid);
        $this->assign('p',$p);
        $this->display();
    }
    
    /**
     * 修改会员等级
     */
    public function updateLevel()
    {
        $uid = I('uid');
        if(!$uid){
            $this->error('传参为空');
        }
        if(IS_POST){
            $level_id = I('post.level_id','','htmlspecialchars');
            // 计算该等级有效天数
            $level_info = M('user_level')->where('id='.$level_id)->field('expri_day,title')->find();
            $days = intval($level_info['expri_day']);
            $data = array(
                'level_id'=>$level_id,
                'expri_day'=>date("Y-m-d",strtotime("$days day"))
            );
            //更新数据库
            $res = M('user')->data($data)->where('id='.$uid)->save();
            if($res){
                addlog('修改会员等级，ID：' . $uid);
                //$this->success('操作成功！',U('updateLevel',array('uid'=>$uid,'close'=>1)));
                redirect(U('updateLevel',array('uid'=>$uid,'close'=>1)));
                die(0);
            }else {
                $this->error('发生异常');
            }
        }else{
            $user_level_list = M('user_level')->order('o asc')->select();
            $user_level = M('user')->where('id='.$uid)->field('level_id')->find();
            $this->assign('user_level_list',$user_level_list);
            $this->assign('level_id',$user_level['level_id']);
            $this->assign('uid',$uid);
            $this->display('form');
        }
    }
    
    /**
     * 批量添加会员动态
     */
    public function batchAddDynamic()
    {
        if(IS_POST){
            $content_arr = array('注册成功！','成功邀请好友！','成功获得任务收益！','成功开通钻石VIP！','成功开通网红！','成功开通黄金VIP！','成功开通铂金VIP！','成功发布任务！');
            $num = intval(I('num',1));
            $data = array();
            for ($i=0;$i<$num;$i++){
                $res['mobile'] = $this->getRandomMobile();
                $res['content'] = $content_arr[array_rand($content_arr,1)];
                $res['t'] = time();
                array_push($data, $res); 
            }
            $ret = M('user_dynamic')->addAll($data);
            if($ret){
                addlog('批量生成会员动态');
                redirect(U('batchAddDynamic',array('close'=>1)));
                die(0);
            }else{
                $this->error('操作失败！');
            }
        }else{
            $this->display('batchForm');  
        }
    }
    
    /**
     * 生成手机号
     * @return string
     */
    private function getRandomMobile()
    {
        $numberPlace = array(30,31,32,33,34,35,36,37,38,39,50,51,58,59,89);
        $mobile = 1;
        $mobile .= $numberPlace[rand(0,count($numberPlace)-1)];
        $mobile .= str_pad(rand(0,99999999),8,0,STR_PAD_LEFT);
        return $mobile;
    }
}